package com.muyu.system.service.impl;

import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.system.domain.DanmoMarketPicture;
import com.muyu.system.domain.DanmoMarketProducts;
import com.muyu.system.mapper.DanmoMarketPictureMapper;
import com.muyu.system.service.IDanmoMarketPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 书画市场图片Service业务层处理
 *
 * @author yangle
 * @date 2024-09-08
 */
@Service
public class DanmoMarketPictureServiceImpl implements IDanmoMarketPictureService
{
    @Autowired
    private DanmoMarketPictureMapper danmoMarketPictureMapper;

    /**
     * 查询书画市场图片
     *
     * @param id 书画市场图片主键
     * @return 书画市场图片
     */
    @Override
    public DanmoMarketPicture selectDanmoMarketPictureById(Long id)
    {
        return danmoMarketPictureMapper.selectDanmoMarketPictureById(id);
    }

    /**
     * 查询书画市场图片列表
     *
     * @param danmoMarketPicture 书画市场图片
     * @return 书画市场图片
     */
    @Override
    public List<DanmoMarketPicture> selectDanmoMarketPictureList(DanmoMarketPicture danmoMarketPicture)
    {
        return danmoMarketPictureMapper.selectDanmoMarketPictureList(danmoMarketPicture);
    }

    /**
     * 新增书画市场图片
     *
     * @param danmoMarketPicture 书画市场图片
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDanmoMarketPicture(DanmoMarketPicture danmoMarketPicture)
    {
        danmoMarketPicture.setCreateTime(DateUtils.getNowDate());
        int rows = danmoMarketPictureMapper.insertDanmoMarketPicture(danmoMarketPicture);
        insertDanmoMarketProducts(danmoMarketPicture);
        return rows;
    }

    /**
     * 修改书画市场图片
     *
     * @param danmoMarketPicture 书画市场图片
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDanmoMarketPicture(DanmoMarketPicture danmoMarketPicture)
    {
        danmoMarketPicture.setUpdateTime(DateUtils.getNowDate());
        danmoMarketPictureMapper.deleteDanmoMarketProductsById(danmoMarketPicture.getId());
        insertDanmoMarketProducts(danmoMarketPicture);
        return danmoMarketPictureMapper.updateDanmoMarketPicture(danmoMarketPicture);
    }

    /**
     * 批量删除书画市场图片
     *
     * @param ids 需要删除的书画市场图片主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoMarketPictureByIds(Long[] ids)
    {
        danmoMarketPictureMapper.deleteDanmoMarketProductsByIds(ids);
        return danmoMarketPictureMapper.deleteDanmoMarketPictureByIds(ids);
    }

    /**
     * 删除书画市场图片信息
     *
     * @param id 书画市场图片主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoMarketPictureById(Long id)
    {
        danmoMarketPictureMapper.deleteDanmoMarketProductsById(id);
        return danmoMarketPictureMapper.deleteDanmoMarketPictureById(id);
    }

    /**
     * 新增书画市场商品信息信息
     *
     * @param danmoMarketPicture 书画市场图片对象
     */
    public void insertDanmoMarketProducts(DanmoMarketPicture danmoMarketPicture)
    {
        List<DanmoMarketProducts> danmoMarketProductsList = danmoMarketPicture.getDanmoMarketProductsList();
        Long id = danmoMarketPicture.getId();
        if (StringUtils.isNotNull(danmoMarketProductsList))
        {
            List<DanmoMarketProducts> list = new ArrayList<DanmoMarketProducts>();
            for (DanmoMarketProducts danmoMarketProducts : danmoMarketProductsList)
            {
                danmoMarketProducts.setId(id);
                list.add(danmoMarketProducts);
            }
            if (list.size() > 0)
            {
                danmoMarketPictureMapper.batchDanmoMarketProducts(list);
            }
        }
    }
}
